import matplotlib.pyplot as plt
import numpy as np

def plot_data(d):
	f = open("data_" + d['metric'] + "_vs_" + d['control'] + ".csv","r")
	lines = [line.strip() for line in f.readlines()]
	f.close() 

	lines = [line.split(",") for line in lines if line]

	i = 0
	data1 = np.array(lines[i][:], dtype=np.float)
	i = i+1
	data2 = np.array(lines[i][:], dtype=np.float)
	i = i+1
	if (d['nn']):
		data3 = np.array(lines[i][:5], dtype=np.float)
		i = i+1
	data4 = np.array(lines[i][:5], dtype=np.float)
	i = i+1
	if (d['euclidean']):
		data5 = np.array(lines[i][:5], dtype=np.float)
		i = i + 1
		data6 = np.array(lines[i][:5], dtype=np.float)
		i = i + 1
	
	y = []
	ywithfull = []

	if (d['control'] == 'dim'):
		y = np.array([10, 50, 100, 500, 1000])
		ywithfull = np.array([10, 50, 100, 500, 1000, 95893])

	if (d['control'] == 'size'):
		y = np.array([100, 500, 1000, 5000, 10000])
		ywithfull = np.array([100, 500, 1000, 5000, 10000])

	if (d['metric'] != 'error'):
		data1 = np.log10(data1)
		data2 = np.log10(data2)
		if (d['nn']):
			data3 = np.log10(data3)
		data4 = np.log10(data4)
		if (d['euclidean']):
			data5 = np.log10(data5)
			data6 = np.log10(data6)

	print data1
	print ywithfull

	plt.plot(np.log10(ywithfull), data1, "b-", label="Exhaustive")
	plt.plot(np.log10(ywithfull), data2, "g-", label="Simple VP")
	if (d['nn']):
		plt.plot(np.log10(y), data3, "r-", label="RP Tree")
	plt.plot(np.log10(y), data4, "y-", label="Random Projection")
	if (d['euclidean']):
		plt.plot(np.log10(y), data5, "k-", label="RBC")
		plt.plot(np.log10(y), data6, "m-", label="LSH")
	fontprop = {'size' : 10}
	plt.legend(loc='lower right', prop=fontprop)

	control = 'Size'
	if (d['control'] == 'dim'): control = 'Dimension'
	metric = 'Time'
	if (d['metric'] == 'mem'): metric = 'Memory'
	elif (d['metric'] == 'error'): metric = 'Error'	

	title = metric + ' vs. ' + control;

	xlabel = 'log(' + control + ')'

	ylabel = ''
	if (d['metric'] == 'mem'):
		ylabel = 'log(' + metric + ') in bytes'
	elif (d['metric'] == 'time'):
		ylabel = 'log(' + metric + ') in seconds'
	elif (d['metric'] == 'error'):
		ylabel = metric
	
	plt.xlabel(xlabel)
	plt.ylabel(ylabel)
	plt.title(title);
	#plt.show()
	fname = 'cossim_'
	if(d['euclidean']):
		fname = 'euclidean_'
	if(d['nn']): fname += '1nn_'
	else: fname += 'knn_'
	fname = fname + d['metric'] + '_' + d['control'] + '.eps'
	plt.savefig(fname)
	plt.close()

euclidean = True
nn = False

plot_data({'euclidean' : euclidean, 'nn' : nn, 
	'metric' : 'error', 'control' : 'size'})

plot_data({'euclidean' : euclidean, 'nn' : nn, 
	'metric' : 'error', 'control' : 'dim'})

plot_data({'euclidean' : euclidean, 'nn' : nn, 
	'metric' : 'time', 'control' : 'size'})

plot_data({'euclidean' : euclidean, 'nn' : nn, 
	'metric' : 'time', 'control' : 'dim'})

plot_data({'euclidean' : euclidean, 'nn' : nn, 
	'metric' : 'mem', 'control' : 'size'})

plot_data({'euclidean' : euclidean, 'nn' : nn, 
	'metric' : 'mem', 'control' : 'dim'})

